<?php
/**
 * 商家中心抽奖活动管理
 *
 *
 *
 ***/


defined('InShopNC') or exit('Access Invalid!');

class lottery_activityControl extends BaseSellerControl {

    public function __construct() {
        parent::__construct();

    }
    /**
     * 默认显示抢购列表
     **/
    public function indexOp() {
        $this->lotteryActivity_listOp();
    }
    
    /**
     * 抢购列表
     **/
    public function lotteryActivity_listOp() {
        $model_activity = Model('lottery_activity');
    
        $condition = array();
        $condition['activity_name'] = array('like', '%'.$_GET['activity_name'].'%');
        $condition['lottery_activity_id']=array('neq',999);
        $list = $model_activity->getActivityExtendList($condition, 10);
        Tpl::output('lists',$list);
        Tpl::output('show_page',$model_activity->showpage());
    
        self::profile_menu('activity_list');
        Tpl::showpage('lottery_activity.list');
    }
    
    /**
     *新增页面
     **/
    public function lotteryActivity_addOp() {
        self::profile_menu('activity_add');
        Tpl::showpage('lottery_activity.add');
    
    }
    
    /**
     *编辑页面
     **/
    public function lotteryActivity_editOp() {
        $activityId = $_GET['activityId'];
        if(!empty($activityId)) {
            $model_activity = Model('lottery_activity');
            $model_gift = Model('lottery_gift');
            
            $condition = array();
            $condition['lottery_activity_id'] = $activityId;
            $activity = $model_activity->getActivityInfo($condition);
           
            Tpl::output('activity', $activity);
            
            $gifts = $model_gift->getGiftExtendList($condition);
            Tpl::output('gifts', $gifts);
        }
        
        
        self::profile_menu('activity_edit');
        Tpl::showpage('lottery_activity.edit');
    
    }
    
    /**
     * 选择活动商品
     **/
    public function search_goodsOp() {
        $model_goods = Model('goods');
        $condition = array();
        $condition['store_id'] = $_SESSION['store_id'];
        $condition['goods_name'] = array('like', '%'.$_GET['goods_name'].'%');
        $goods_list = $model_goods->getGoodsCommonListForPromotion($condition, '*', 8, 'groupbuy');
    
        Tpl::output('goods_list', $goods_list);
        Tpl::output('show_page', $model_goods->showpage());
        Tpl::showpage('lottery_activity.goods', 'null_layout');
    }
    
    /**
     * 选择优惠券
     **/
    public function search_voucherOp() {
        $model_voucher = Model('voucher');
        $condition = array();
        $condition['voucher_t_store_id'] = $_SESSION['store_id'];
        $condition['voucher_t_title'] = array('like', '%'.$_GET['voucher_title'].'%');
        $condition['voucher_t_state'] = 1;
        $voucher_list=$model_voucher->getVoucherTemplateList($condition, '*', 8 );
        
    
        Tpl::output('voucher_list', $voucher_list);
        Tpl::output('show_page', $model_voucher->showpage());
        Tpl::showpage('store_voucher.select', 'null_layout');
    }
    
    /**
     * 选择优惠券(获取优惠券信息)
     **/
    public function voucher_infoOp() {
        $model_voucher = Model('voucher');
        $condition = array();
        $condition['voucher_t_store_id'] = $_SESSION['store_id'];
        $condition['voucher_t_id'] = $_GET['voucher_id'];
        $voucher_list = $model_voucher->table('voucher_template')->where($condition)->find();
        
        $voucher_list['result'] = true;
        if (!$voucher_list['voucher_t_customimg'] || !file_exists(BASE_UPLOAD_PATH.DS.ATTACH_VOUCHER.DS.$_SESSION['store_id'].DS.$voucher_list['voucher_t_customimg'])){
	        	$voucher_list['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.defaultGoodsImage(240);
	    }else{
	        	$voucher_list['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.ATTACH_VOUCHER.DS.$_SESSION['store_id'].DS.str_ireplace('.', '_small.', $voucher_list['voucher_t_customimg']);
	    }
        echo json_encode($voucher_list);die;
    }
    
    public function list_goods_info() {
        $goods_commonid = intval($_GET['goods_commonid']);
    
        $data = array();
        $data['result'] = true;
    
        $model_goods = Model('goods');
    
        $condition = array();
        $condition['goods_commonid'] = $goods_commonid;
        $goods_list = $model_goods->getGoodsOnlineList($condition);
    
        if(empty($goods_list)) {
            $data['result'] = false;
            $data['message'] = L('param_error');
            echo json_encode($data);die;
        }
    
        $goods_info = $goods_list[0];
        $data['goods_id'] = $goods_info['goods_id'];
        $data['goods_name'] = $goods_info['goods_name'];
        $data['goods_price'] = $goods_info['goods_price'];
        $data['goods_image'] = thumb($goods_info, 240);
        $data['goods_href'] = urlShop('goods', 'index', array('goods_id' => $goods_info['goods_id']));
    
        if ($goods_info['is_virtual']) {
            $data['is_virtual'] = 1;
            $data['virtual_indate'] = $goods_info['virtual_indate'];
            $data['virtual_indate_str'] = date('Y-m-d H:i', $goods_info['virtual_indate']);
            $data['virtual_limit'] = $goods_info['virtual_limit'];
        }
    
        echo json_encode($data);die;
    }
    
    /**
     * 上传图片
     **/
    public function image_uploadOp() {
        if(!empty($_POST['old_activity_image'])) {
            $this->_image_del($_POST['old_activity_image']);
        }
        $this->_image_upload('activity_image');
    }
    
    private function _image_upload($file) {
        $data = array();
        $data['result'] = true;
        if(!empty($_FILES[$file]['name'])) {
            $upload	= new UploadFile();
            $uploaddir = ATTACH_PATH.DS.'activity'.DS.$_SESSION['store_id'].DS;
            $upload->set('default_dir', $uploaddir);
            $upload->set('fprefix', $_SESSION['store_id']);
            $result = $upload->upfile($file);
            if($result) {
                $data['file_name'] = $upload->file_name;
                $data['origin_file_name'] = $_FILES[$file]['name'];
                $data['file_url'] = UPLOAD_SITE_URL.DS.ATTACH_ACTIVITY.DS.$_SESSION['store_id'].DS.$upload->file_name;
            } else {
                $data['result'] = false;
                $data['message'] = $upload->error;
            }
        } else {
            $data['result'] = false;
        }
        echo json_encode($data);die;
    }
    
    /**
     * 抢购保存
     **/
    public function activity_saveOp() {
        $model_activity = Model('lottery_activity');
        $model_gift = Model('lottery_gift');
        
        $param = array();
        $param['lottery_activity_id'] = $_POST['activity_id'];
        $param['activity_name'] = $_POST['activity_name'];
        $param['exchange_start_date'] = strtotime($_POST['exchange_start_date']);
        $param['exchange_end_date'] = strtotime($_POST['exchange_end_date']);
        
        /*$param['comments'] = $_POST['comments'];
        $upload		= new UploadFile();
        if($_FILES['image']['name'] != '' ){
        
            $upload->set('default_dir',ATTACH_ACTIVITY);
            //读取UPLOAD_SITE_URL.'/'.ATTACH_ACTIVITY.'/'.$info['image'];
            $result = $upload->upfile('image');
            if (!$result){
                showMessage($upload->error,'','','error');
            }
            $param['image'] = $upload->file_name;
        
        } */
        
        
        if(empty($_POST['goods_ids'])) {
            showDialog('请添加礼品!');
            return;
        }
        $goods_ids = explode(',', $_POST['goods_ids']);
        $short_names = explode(',', $_POST['short_names']);
        $pro_nums = explode(',', $_POST['pro_nums']);
        $gift_ids = explode(',', $_POST['gift_ids']);
        
        //保存
        $result = $model_activity->addActivity($param);
        
        if($result) {
            for($i=0,$j=count($goods_ids); $i<$j; $i++) {
                $condition = array();
                $condition["lottery_gift_id"] = intval($gift_ids[$i]);
                $condition["lottery_activity_id"] = intval($_POST['activity_id']);
                
                $obj_id = explode('|', $goods_ids[$i]);
                $condition["goods_id"] = intval($obj_id[0]);
                $condition["type"] = intval($obj_id[1]);
                
                $condition["gift_num"] = intval($pro_nums[$i]);
                $condition["remain_num"] = intval($pro_nums[$i]);
                $condition["gift_show_name"] = $short_names[$i];
                
                $result = $model_gift->addGift($condition);
            }
            if($result) {
                showDialog("抽奖活动发布成功!",'index.php?act=lottery_activity','succ');
            } else {
                showDialog("保存失败!");
                return;
            }
        } else {
           showDialog("保存失败!");
           return;
        }
    }
    
    /**
     * 保存
     **/
    public function activity_edit_saveOp() {
        $model_activity = Model('lottery_activity');
        $model_gift = Model('lottery_gift');
        
        $activity_id = $_POST['activity_id'];
        
        $activity = $model_activity->where(array('lottery_activity_id' => $activity_id))->find();
    
        $activity['activity_name'] = $_POST['activity_name'];
        $activity['exchange_start_date'] = strtotime($_POST['exchange_start_date']);
        $activity['exchange_end_date'] = strtotime($_POST['exchange_end_date']);
        
        /* $activity['comments'] = $_POST['comments'];
        $upload		= new UploadFile();
        if($_FILES['image']['name'] != '' ){
        
            $upload->set('default_dir',ATTACH_ACTIVITY);
            //读取UPLOAD_SITE_URL.'/'.ATTACH_ACTIVITY.'/'.$info['image'];
            $result = $upload->upfile('image');
            if (!$result){
                showMessage($upload->error,'','','error');
            }
            $activity['image'] = $upload->file_name;
        
        } */
        
        if(empty($_POST['goods_ids'])) {
            showDialog('请添加奖品!');
            return;
        }
        $goods_ids = explode(',', $_POST['goods_ids']);
        $short_names = explode(',', $_POST['short_names']);
        $pro_nums = explode(',', $_POST['pro_nums']);
        $gift_ids = explode(',', $_POST['gift_ids']);
    
        //保存
        $result = $model_activity->editActivityById($activity,$activity_id);
        
        //删除所有的奖品
        $con = array();
        $con['lottery_activity_id'] = $activity_id;
        $result = $model_gift->delGift($con);
    
        if($result) {
            for($i=0,$j=count($goods_ids); $i<$j; $i++) {
                $condition = array();
                $condition["lottery_gift_id"] = intval($gift_ids[$i]);
                $condition["lottery_activity_id"] = intval($activity_id);
                
                $obj_id = explode('|', $goods_ids[$i]);
                $condition["goods_id"] = intval($obj_id[0]);
                $condition["type"] = intval($obj_id[1]);
                
                $condition["gift_num"] = intval($pro_nums[$i]);
                $condition["remain_num"] = intval($pro_nums[$i]);
                $condition["gift_show_name"] = $short_names[$i];
    
                $result = $model_gift->addGift($condition);
            }
            if($result) {
                showDialog("抽奖活动修改成功!",'index.php?act=lottery_activity','succ');
            } else {
                showDialog("保存失败!");
                return;
            }
        } else {
            showDialog("保存失败!");
            return;
        }
    }
    
    public function drop_activityOp() {
        $model_activity = Model('lottery_activity');
        $model_gift = Model('lottery_gift');
        
        $activityId = intval($_GET['activityId']);
        if(!empty($activityId)) {
            $condition = array();
            $condition['lottery_activity_id'] = $activityId;
            if($model_gift->delGift($condition)) {
                $result = $model_activity->delActivity($condition);
                if($result) {
                    showDialog("活动删除成功!",'index.php?act=lottery_activity','succ');
                } else {
                    showDialog("删除失败!");
                    return;
                }
            }
            
        }
    }

    
    /**
     * 用户中心右边，小导航
     *
     * @param string 	$menu_key	当前导航的menu_key
     * @param array 	$array		附加菜单
     * @return
     */
    private function profile_menu($menu_key='') {
        $menu_array	= array(
            1=>array('menu_key'=>'activity_list','menu_name'=>"礼品活动列表",'menu_url'=>urlShop('lottery_activity', 'lotteryActivity_list'))
        );
        switch ($menu_key){
            case 'activity_add':
                $menu_array[] = array('menu_key'=>'activity_add','menu_name'=>'新增礼品','menu_url'=>'index.php?act=lottery_activity&op=lotteryActivity_add');
                break;
            case 'activity_edit':
                $menu_array[] = array('menu_key'=>'activity_edit','menu_name'=>'编辑礼品','menu_url'=>'index.php?act=lottery_activity&op=lotteryActivity_edit');
                break;
            case 'cancel':
                $menu_array[] = array('menu_key'=>'activity_cancel','menu_name'=>L('nc_member_path_cancel_group'));
                break;
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }
}
